// ---------------------------------------------------------------------
//
//	Copyright 2003 e Professional Co.,Ltd.  All Rights Reserved.
//
//	File : manageWindow.js
//
//	Description : The manageWindow javascript  provides an easy way
//				to manage windows etc. open, close, focus windows and 
//				capture events on that window for security of web applications. 
//				It provides a variety of functions to customize the window, that 
//				will suitable for each project.
//
//-------------------------------------------------------------------- 


// Variable for check children window
var newWindow;


//------------------------------------------------------------------------
//
//	Function : openWindow
//
//	Synopsis : This function is use for open file in new window (normal window).
//				Opened window will have property like parent window. 
//
//	Arguments : This function receive 7 arguments (1 required) 
//			*	linkFile - String that specifies the URL of the document to display. 
//				windowWidth - Number that sets the width of the window, in pixels. 
//				windowHeight - Number that sets the height of the window, in pixels. 
//				features - String parameter is a list of items separated by commas. 
//									Each item consists of an option and a value. 
//				name - String that specifies the name of the window. 
//				leftPos - String or Number that specifies the left position of the window. 
//				topPos - String or Number that specifies the top position of the window. 
//
//	Returns : Returns a reference to the new window object.
//
//	Notes : none
//
//------------------------------------------------------------------------
function openWindow(linkFile, windowWidth, windowHeight, features, name, leftPos, topPos) {
// Set default value for run function
	defaultWidth = window.screen.width; // Default width of new window
	defaultHeight = window.screen.height; // Default Height of new window
	defaultFeatures = "menubar=no, toolbar=no, location=no, directory=no, status=no"; // Default features of new window
	defaultName = ""; // Default name of new window

// Check value of arguments and assign appropriate value
	windowWidth = (windowWidth == undefined || windowWidth == "") ? defaultWidth : windowWidth ;
	windowHeight = (windowHeight == undefined || windowHeight == "") ? defaultHeight : windowHeight ;
	features = (features == undefined || features == "") ? defaultFeatures : features ;
	name = (name == undefined || name == "") ? defaultName : name ;

// Find left and top position of window
	leftPos = findLeftPosition(leftPos, windowWidth);
	topPos = findTopPosition(topPos, windowHeight);

// Open new window then resize and move window
	newWindow = window.open(linkFile, name, features);
	newWindow.resizeTo(windowWidth, windowHeight);
	newWindow.moveTo(leftPos, topPos);
}


//------------------------------------------------------------------------
//
//	Function : resizeWindow
//
//	Synopsis : This function is use for change window size and
//				move window to appropriate position. 
//
//	Arguments : This function receive 5 arguments (3 required) 
//			*	winObj - Object that is window object. 
//			*	windowWidth - Number that sets the width of the window, in pixels. 
//			*	windowHeight - Number that sets the height of the window, in pixels. 
//				leftPos - String or Number that specifies the left position of the window. 
//				topPos - String or Number that specifies the top position of the window. 
//
//	Returns : none
//
//	Notes : none
//
//------------------------------------------------------------------------
function resizeWindow(winObj, windowWidth, windowHeight, leftPos, topPos) {
// Resize window
	winObj.resizeTo(windowWidth, windowHeight);

// Find left and top position of window and move window
	leftPos = findLeftPosition(leftPos, windowWidth);
	topPos = findTopPosition(topPos, windowHeight);
	winObj.moveTo(leftPos, topPos);
}


//------------------------------------------------------------------------
//
//	Function : findLeftPosition
//
//	Synopsis : This function is use for find value of left postion of window. 
//
//	Arguments : This function receive 2 arguments (2 required) 
//			*	leftPos - String or Number that specifies the left position of the window. 
//			*	windowWidth - Number that is the width of the window, in pixels. 
//
//	Returns : Number of left postion of window.
//
//	Notes : none
//
//------------------------------------------------------------------------
function findLeftPosition(leftPos, windowWidth) {
// Set default value for run function
	defaultWidth = window.screen.width; // Default width of window
	defaultLeftPos = (defaultWidth-windowWidth)/2; // Default left position of window

// Check value of arguments and assign appropriate value
	leftPos = (leftPos == undefined || leftPos == "") ? ""+defaultLeftPos : ""+leftPos ;

// Check left position of window
	if (leftPos.toLowerCase() == "left") {
		leftPos = 0;
	} else if (leftPos.toLowerCase() == "center") {
		leftPos = (defaultWidth-windowWidth)/2;
	} else if (leftPos.toLowerCase() == "right") {
		leftPos = defaultWidth-windowWidth;
	}

	return leftPos;
}


//------------------------------------------------------------------------
//
//	Function : findTopPosition
//
//	Synopsis : This function is use for find value of top postion of window. 
//
//	Arguments : This function receive 2 arguments (2 required) 
//			*	topPos - String or Number that specifies the top position of the window. 
//			*	windowHeight - Number that is the height of the window, in pixels. 
//
//	Returns : Number of top postion of window.
//
//	Notes : none
//
//------------------------------------------------------------------------
function findTopPosition(topPos, windowHeight) {
// Set default value for run function
	defaultHeight = window.screen.height; // Default Height of window
	defaultTopPos = (defaultHeight-windowHeight)/2; // Default top position of window

// Check value of arguments and assign appropriate value
	topPos = (topPos == undefined || topPos == "") ? ""+defaultTopPos : ""+topPos ;

// Check top position of window
	if (topPos.toLowerCase() == "top") {
		topPos = 0;
	} else if (topPos.toLowerCase() == "middle") {
		topPos = (defaultHeight-windowHeight)/2;
	} else if (topPos.toLowerCase() == "bottom") {
		topPos = defaultHeight-windowHeight;
	}

	return topPos;
}


//------------------------------------------------------------------------
//
//	Function : checkFocus
//
//	Synopsis : This function is use for focus in suitable window
//				(In case of window have children).
//
//	Arguments : none 
//
//	Returns : none
//
//	Notes : none
//
//------------------------------------------------------------------------
function checkFocus() {
// Check window have children or not
	if (newWindow != undefined) {
		window.blur();
		newWindow.focus();
	}
}


// Variable for close window
var closeMainWindow = true; // Use for close main window
var closeChildWindow = true; // Use for close child window
var closeModalWindow = true; // Use for close modal window
var goToMenu = ""; // Default menu file


//------------------------------------------------------------------------
//
//	Function : closeMainWindowOrNot
//
//	Synopsis : This function is use for close(not close) main window
//				(In case of window have children).
//
//	Arguments : This function receive 1 arguments (0 required) 
//				contentFrameName - String that specifies frame name that contain content of data. 
//
//	Returns : none
//
//	Notes : The web page that call this function is the child window and 
//				it should be set value of closeMainWindow and goToMenu.
//
//------------------------------------------------------------------------
function closeMainWindowOrNot(contentFrameName) {
// Set default value for run function
	defaultFrameName = "Menu"; // Default content frame name

// Check value of arguments and assign appropriate value
	contentFrameName = (contentFrameName == undefined || contentFrameName == "") ? defaultFrameName : contentFrameName ;

	if(closeMainWindow) {
		window.opener.close();
	} else {
		if (goToMenu != "") {
			eval("window.opener."+contentFrameName).location.href = goToMenu;		
		}
		window.opener.focus();
	}
}


//------------------------------------------------------------------------
//
//	Function : closeChildWindowOrNot
//
//	Synopsis : This function is use for close(not close) child window
//				(In case of window have children).
//
//	Arguments : none 
//
//	Returns : String that is alert message or return true
//
//	Notes : The web page that call this function is the child window and 
//				it should be set value of closeChildWindow and should call from
//				onBeforeUnload event.
//
//------------------------------------------------------------------------
function closeChildWindowOrNot() {
	if(closeChildWindow) {
		return 'Close Program ?';
	}
}


//------------------------------------------------------------------------
//
//	Function : closeModalWindowOrNot
//
//	Synopsis : This function is use for close(not close) modal window
//
//	Arguments : none 
//
//	Returns : String that is alert message or return true
//
//	Notes : The web page that call this function is the child window and 
//				it should be set value of closeModalWindow and should call from
//				onBeforeUnload event.
//
//------------------------------------------------------------------------
function closeModalWindowOrNot() {
	if(!closeModalWindow) {
		closeChildWindow = closeModalWindow;
	}
	return closeChildWindowOrNot();
}


//------------------------------------------------------------------------
//
//	Function : checkKey
//
//	Synopsis : This function is use for check key that check user press on page
//				and not permit user press some key.
//
//	Arguments : none 
//
//	Returns : Boolean that (not)permit user press key
//
//	Notes : Used of this function is should depend on user requirement. This function
//				want some function in file alert.js
//
//------------------------------------------------------------------------
function checkKey() {
// Variable for return value
returnValue = true;

// Use Ctrl key only in Ctrl+A, Ctrl+C, Ctrl+V, Ctrl+X
	if (event.ctrlKey) {
		if (event.ctrlKey && (event.keyCode==65 || event.keyCode==67 || event.keyCode==86 || event.keyCode==88)) {
			returnValue =  true;
		} else {
			returnValue =  false;	
		}
	}

// Don't use Alt key
	if (event.altKey) {
		showOWarningDialog("Don't press key", "Don't use Alt Key", "OK");
		returnValue =  false;
	}

//Don't use Backspace Key except in text field or text area
	if ((event.srcElement.tagName).toUpperCase() == "INPUT" || (event.srcElement.tagName).toUpperCase() == "TEXTAREA") {
			returnValue =  true;
	} else {
		if (event.keyCode==8) {
			showOWarningDialog("Don't press key", "Don't use Backspace Key", "OK");
			returnValue =  false;
		}
	}

//Don't use F1-F12 -- have bug
/*
	if (event.keyCode>=112 && event.keyCode<=123) {
		alert("Don't use F1-F12 Key");
		returnValue =  false;
	}
*/

return returnValue;
}


//------------------------------------------------------------------------
//
//	Function : openModalWindow
//
//	Synopsis : This function is use for open file in new window (modal window).
//
//	Arguments : This function receive 7 arguments (1 required) 
//			*	linkFile - String that specifies the URL of the document to display. 
//				windowWidth - Number that sets the width of the window, in pixels. 
//				windowHeight - Number that sets the height of the window, in pixels. 
//				features - String parameter is a list of items separated by commas. 
//									Each item consists of an option and a value. 
//				name - String that specifies the name of the window. 
//				leftPos - String or Number that specifies the left position of the window. 
//				topPos - String or Number that specifies the top position of the window. 
//
//	Returns : none
//
//	Notes : none
//
//------------------------------------------------------------------------
function openModalWindow(linkFile, windowWidth, windowHeight, features, reference, leftPos, topPos) {
// Set default value for run function
	defaultWidth = window.screen.width; // Default modal of new window
	defaultHeight = window.screen.height; // Default Height of modal window
	defaultFeatures = "help: no; resizable: no; scroll: no; status: no; "; // Default features of modal window
	defaultReference = window; // Default reference value of window

// Check value of arguments and assign appropriate value
	windowWidth = (windowWidth == undefined || windowWidth == "") ? defaultWidth : windowWidth ;
	windowHeight = (windowHeight == undefined || windowHeight == "") ? defaultHeight : windowHeight ;
	features = (features == undefined || features == "") ? defaultFeatures : features ;
	reference = (reference == undefined || reference == "") ? defaultReference : reference ;

// Find left and top position of window
	leftPos = findLeftPosition(leftPos, windowWidth);
	topPos = findTopPosition(topPos, windowHeight);

	features = features+"dialogWidth: "+windowWidth+"px; dialogHeight: "+windowHeight+"px; dialogLeft: "+leftPos+"px; dialogTop: "+topPos+"px; "

// Open modal window
	window.showModalDialog(linkFile, window, features);
}
